import os,shutil,function
import mysql.connector
from datetime import datetime


mydb = mysql.connector.connect(
  host="192.168.10.81",
  user="root",
  passwd="",
  database="xiaocheng"
)
# 获取最大id
mycursor = mydb.cursor()
mycursor.execute("select max(id) from first_draft") 
myresult = mycursor.fetchone()
begin_id=myresult[0]+1


pic_path = 'D:/Desk/烫画文件/原图/'
pic_paths = function.get_file_paths(pic_path)

now = datetime.now()
formatted_date = now.strftime("%Y-%m-%d")

datas=[]
i=0
for pic_path in pic_paths:
    file_name=os.path.basename(pic_path)
    pic_path=pic_path.replace('\\','/')
    name='CG' +str(begin_id + 1000 + i)
    pic=name+os.path.splitext(file_name)[1]
    name_original=file_name
    dict=[] 
    new_path="Y:/pic/first_draft/"+pic
    shutil.copy(pic_path,new_path)     
    dict.append(name)
    dict.append(pic)
    dict.append(name_original)
    dict.append(formatted_date)  
    datas.append(tuple(dict))
    print(tuple(dict))
    i+=1
print(i)


# 插入数据
sql = "insert into first_draft (name,pic,pic_original,date) values (%s,%s,%s,%s)" 
mycursor.executemany(sql, datas) 
mydb.commit()    # 数据表内容有更新，必须使用到该语句
 
print(mycursor.rowcount, "记录插入成功。")

mycursor.close()
mydb.close()